# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : __new__单例模式(日志记录器).py
# @Author: dongguangwen
# @Date  : 2025-03-29 22:35
class Singleton(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super().__new__(cls)
        return cls.__instance


class Logger(Singleton):
    def __init__(self, log_file="app.log"):
        if not hasattr(self, 'file_handle'):
            self.file_handle = open(log_file, 'a')

    def log(self, message):
        self.file_handle.write(message + "\n")


# 所有模块共享同一日志器
logger1 = Logger()
logger2 = Logger()
logger1.log("Test")  # 写入到同一文件

"""
统一管理日志写入，防止多实例竞争文件资源。
"""